home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / a_utils / perl / prlrf435.lha / parr < prev    next >
Text File  |  1992-02-18  |  4KB  |  193 lines

  1. #!/usr/bin/perl
  2.  
  3. # @(#)@ parr 4.2.3
  4.  
  5. # rearrange conforming PS code to print the pages in an arbitrary
  6. # order.  The -o option takes a list of ranges, like this:
  7. #    1-5    1-10,11-20    11-,1-10
  8. # usage: parr [-a4] [-o list] [file]
  9. #
  10. # jgreely@cis.ohio-state.edu, 89/10/23
  11. # modified by jv@mh.nl, 91/078/15
  12.  
  13. $order='';
  14. $signFlag='';
  15. $signCount=0;
  16. $DEBUG=0;
  17. $TMPDIR='/usr/tmp';
  18.  
  19. while ($_ = $ARGV[0],/^-/) {
  20.     shift;
  21.     last if /^-\-$/;
  22.     /^-o(rder)?$/ && ($order = shift,next);
  23.     /^-a4$/ && ($a4flag++,next);
  24.     die "usage: parr [-a4] [-order list] [file]\n";
  25. }
  26.  
  27. $file = "$TMPDIR/p$$.header";
  28. @files = ($file);
  29. $sheet=0;
  30. open(FILE,">$file") || die "$file: $!\n";
  31. while (<>) {
  32.     #
  33.     # hack to use NeXT Preview: strip old '%%Pages:' lines
  34.     #
  35.     next if /^%%Pages:/;
  36.     if (/^%%Page:/) {
  37.         $sheet++;
  38.         $pagemap{$sheet} = $1 if /%%Page:\s+(\S+)\s+\S+/;
  39.         close(FILE);
  40.         $file = "$TMPDIR/p$$.$sheet";
  41.         push(@files,$file);
  42.         open(FILE,">$file") || die "$file: $!\n";
  43.     }
  44.     if (/^%%Trailer/) {
  45.         close(FILE);
  46.         $file = "$TMPDIR/p$$.trailer";
  47.         push(@files,$file);
  48.         open(FILE,">$file") || die "$file: $!\n";
  49.     }
  50.     if (/^TeXDict begin @(a4|letter)/) {
  51.         # Insert twoup before switching to TeXDict
  52.         &twoup;
  53.         $twoup++;
  54.         &double_sided;
  55.         print FILE "TeXDict begin @landscape\n";
  56.         next;
  57.     }
  58.     print FILE $_;
  59. }
  60. close(FILE);
  61. die "twoup insertion error\n" unless $twoup == 1;
  62.  
  63. @order = ();
  64. if ($order) {
  65.     foreach $range (split(/,/,$order)) {
  66.         ($start,$sep,$end) = split(/(-)/,$range);
  67.         $start = 1 unless $start;
  68.         $end = $sheet unless $end;
  69.         if ($sep) {
  70.             push(@order,$start..$end);
  71.         }else{
  72.             push(@order,$start);
  73.         }
  74.     }
  75. }elsif ($signFlag) {
  76.     if (! $signCount) {
  77.         $signCount = $sheet;
  78.         $signCount += (4 - $sheet % 4) if ($sheet % 4);
  79.     }else{
  80.         $signCount *=4;
  81.     }
  82.     for($base=0;$base<$sheet;$base+=$signCount) {
  83.         @tmp = ($signCount/2+$base);
  84.         push(@tmp,$tmp[0]+1,$tmp[0]+2,$tmp[0]-1);
  85.         while ($tmp[3] > $base) {
  86.             push(@order,@tmp);
  87.             @tmp = ($tmp[0]-2,$tmp[1]+2,$tmp[2]+2,$tmp[3]-2);
  88.         }
  89.     }
  90. }else{
  91.     @order = (1..$sheet);
  92. }
  93.  
  94. @tmp=@order;
  95. @order=();
  96. foreach $page (@tmp) {
  97.     push(@order,$page > $sheet ? "B" : $page);
  98. }
  99.  
  100. open(FILE,"$TMPDIR/p$$.header");
  101. $_ = <FILE>;
  102. print $_,"%%Pages: (atend)\n";
  103. print while <FILE>;
  104. close(FILE);
  105.  
  106. $count = 0;
  107. foreach $page (@order) {
  108.     $count++;
  109.     $num = "?";
  110.     $num = $pagemap{$page} if defined $pagemap{$page};
  111.     $num .= "/" . $pagemap{$order[$count]} 
  112.         if defined $pagemap{$order[$count]};
  113.     print "%%Page: $num ", ($count+1)/2, "\n" if $count & 1;
  114.     print "%%OldPage: $page\n";
  115.     if ($page eq "B") {
  116.         print "showpage\n";
  117.     }else{
  118.         open(FILE,"$TMPDIR/p$$.$page");
  119.         while (<FILE>) {
  120.             print unless /^%%Page:/;
  121.         }
  122.         close(FILE);
  123.     }
  124. }
  125. open(FILE,"$TMPDIR/p$$.trailer");
  126. print while <FILE>;
  127. close(FILE);
  128. print "%%Pages: ", int(($count+1)/2), " 0\n";
  129.  
  130. unlink @files unless $DEBUG;
  131. exit(0);
  132.  
  133. sub twoup {
  134. $factor = 0.707106781187;
  135.  
  136. # Measurements are in 1/100 inch approx.
  137. if ( $a4flag) {
  138.     $topmargin = -57;
  139.     $leftmargin = 28;
  140.     $othermargin = 445;    # do not change -- relative to $leftmargin
  141. }
  142. else {
  143.     $topmargin = -30;
  144.     $leftmargin = 0;
  145.     $othermargin = 445;    # do not change -- relative to $leftmargin
  146. }
  147. $vsize = -1 - $factor;
  148. print FILE <<EOD;
  149. /isoddpage true def
  150. /orig-showpage /showpage load def
  151. /showpage {
  152.         isoddpage not { orig-showpage } if
  153.         /isoddpage isoddpage not store 
  154.     } def
  155.  
  156. /bop-hook {
  157.     /vsize $vsize def
  158.         isoddpage 
  159.     { $factor $factor scale $topmargin $leftmargin translate }
  160.         { 0 $othermargin translate}
  161.     ifelse
  162.     } def
  163.  
  164. /end-hook{ isoddpage not { orig-showpage } if } def
  165. EOD
  166. }
  167.  
  168. sub double_sided {
  169.  
  170.     # From: Tim Huckvale <tjh@praxis.co.uk>
  171.     #
  172.     # You may be interested in the following problem, and fix, that we
  173.     # found when attempting to print the reference card on our Hewlett
  174.     # Packard Laser-Jet IIISi printer.
  175.     # 
  176.     # On this printer, refguide.ps prints double-sided with the
  177.     # reverse side of each sheet upside down.  We fixed it with the
  178.     # following patch, applied before printing.
  179.  
  180.     # From: Johan Vromans <jv@mh.nl>
  181.     #
  182.     # Okay -- consider this an unsupported feature.
  183.  
  184.     print FILE <<EOD;
  185. statusdict /setduplexmode known { statusdict begin true setduplexmode end } if
  186. statusdict /settumble known { statusdict begin true settumble end } if
  187. EOD
  188. }
  189.  
  190. # Local Variables:
  191. # mode:(perl)
  192. # End:
  193.